package util;

import java.util.StringTokenizer;

public class Gene {
	private long start;
	private long finish;
	private int direction;
	private String sequence;
	private String organism;

	public Gene(String annotation,String sequence) {
		StringTokenizer t = new StringTokenizer(annotation, ":");
		organism=t.nextToken();
		String token = t.nextToken();
		t = new StringTokenizer(token, "-");
		token = t.nextToken();
		if (token.charAt(0) == 'c') {
			direction = -1;
			finish = Long.valueOf(token.substring(1));
			start = Long.valueOf(t.nextToken());
		} else {
			direction = 1;
			start = Long.valueOf(token);
			finish = Long.valueOf(t.nextToken());
		}
		this.sequence=sequence;
	}

	public String getOrganism(){
		return organism;
	}
	public boolean overlap(Gene g) {
		/*if (!g.getOrganism().equals(organism)){
			return false;
		}*/
		if (g.direction == direction)
			return false;
		if (finish < g.start)
			return false;
		if (g.finish < start)
			return false;
		long[] coordinates=overlapCoordinates(g);
		if ((coordinates[1]-coordinates[0])%3!=0 ||(coordinates[1]-coordinates[0])==0 )
			return false;
		return true;
	}
	
	public long[] overlapCoordinates(Gene g){
		long[] toReturn=new long[2];
		if (start<g.start){
			if (g.finish<=finish){
				toReturn[0]=g.start;
				toReturn[1]=g.finish;
			}else{
				toReturn[0]=g.start;
				toReturn[1]=finish;
			}
		}
		else{
			if (g.finish<=finish){
				toReturn[0]=start;
				toReturn[1]=g.finish;
			}else{
				toReturn[0]=start;
				toReturn[1]=finish;
			}
		}		
		return toReturn;
	}
	
	public long getStart(){
		return start;
	}
	public long getFinish(){
		return finish;
	}
	public int getDirection(){
		return direction;
	}
	public String getSequence(){
		return sequence;
	}
	public String getSequenceNotComplementary(long begin,long end){	
		int a=(int) (begin-start);
		int b=(int) (end-start);
		return sequence.substring(a,b);
	}
	public String getComplementarySequence(String sequence){
		String toReturn="";
		for (int i=sequence.length()-1;i>-1;i--){
			if (sequence.charAt(i)=='A'){
				toReturn=toReturn+'T';
			}else{
				if (sequence.charAt(i)=='T'){
					toReturn=toReturn+'A';
				}else{
					if (sequence.charAt(i)=='C'){
						toReturn=toReturn+'G';
					}else
						toReturn=toReturn+'C';
				}
				
			}
		}
		return toReturn;
	}
}
